const { Handover, Content, User, Store, sequelize } = require('../common/mysql.js')
const { Op } = require('sequelize')

module.exports = {
    async insertHandover(handover) {
        return sequelize.transaction(t => {
            return Handover.create(handover, {
                transaction: t
            }).then(insertedHandover => {
                handover.Contents.map(content => {
                    content.handover_id = insertedHandover.id
                })
                return Content.bulkCreate(handover.Contents, {
                    transaction: t
                })
            })
        })
    },
    async getHandover(id, store_id) {
        return await Handover.findByPk(id, {
            include: [
                {
                    model: Content
                },
                {
                    model: User,
                    where: {
                        store_id: {
                            [Op.eq]: store_id
                        }
                    },
                    attributes: ['id', 'name']
                }
            ]
        })
    },
    async checkHandover(id, handover, store_id) {
        const where = {
            date: {
                [Op.eq]: handover.date
            },
            shift: {
                [Op.eq]: handover.shift
            }
        }
        if (id) {
            where.id = {
                [Op.ne]: id
            }
        }
        return await Handover.findOne({
            include: [
                {
                    model: User,
                    where: {
                        store_id: {
                            [Op.eq]: store_id
                        }
                    },
                    attributes: []
                }
            ],
            where: where
        })
    },
    async updateHandover(id, handover) {
        let transaction
        try {
            transaction = await sequelize.transaction()
            await Handover.update(handover, {
                where: {
                    id: {
                        [Op.eq]: id
                    }
                },
                fields: ['date', 'shift', 'nr_begin', 'nr_end', 'temperature', 'humidity'],
                transaction
            })
            for (let i = 0; i < handover.Contents.length; i++) {
                if (handover.Contents[i].id) {
                    await Content.update(handover.Contents[i], {
                        where: {
                            id: {
                                [Op.eq]: handover.Contents[i].id
                            }
                        },
                        fields: ['content'],
                        transaction
                    })
                } else {
                    handover.Contents[i].handover_id = id
                    await Content.create(handover.Contents[i], {
                        transaction
                    })
                }
            }
            await transaction.commit()
        } catch (e) {
            await transaction.rollback()
            throw new Error('更新失败')
        }
    },
    async getHandoverList(store_id, request) {
        const { page, pageSize, begin, end, shift, user, content, prop, order } = request
        const start = (page - 1) * pageSize
        const handoverCondition = {}
        if (begin && end) {
            handoverCondition.date = {
                [Op.between]: [begin, end]
            }
        } else if (begin) {
            handoverCondition.date = {
                [Op.gte]: begin
            }
        } else if (end) {
            handoverCondition.date = {
                [Op.lte]: end
            }
        }
        if (shift) {
            handoverCondition.shift = {
                [Op.eq]: shift
            }
        }
        if (user) {
            handoverCondition.user_id = {
                [Op.eq]: user
            }
        }

        const storeCondition = {}
        if (store_id) {
            storeCondition.store_id = {
                [Op.eq]: store_id
            }
        }
        const orderByCondition = []
        if (prop && order) {
            orderByCondition.push([prop, order])
        }

        const where = {
            include: [
                {
                    model: User,
                    where: storeCondition,
                    attributes: ['id', 'name', 'store_id'],
                    include: [
                        {
                            model: Store
                        }
                    ]
                }
            ],
            where: handoverCondition,
            order: orderByCondition,
            offset: start ? start : 0,
            limit: pageSize ? Number(pageSize) : null,
            distinct: true
        }

        if (content) {
            where.include.push({
                model: Content,
                where: {
                    content: {
                        [Op.like]: `%${content}%`
                    }
                }
            })
        }
        return await Handover.findAndCountAll(where)
    }
}
// export async function insertHandover(handover){
//     let transaction;
//     try {
//         transaction = await mysql.transaction();
//         const insertedHandover = await Handover.create(handover, transaction)
//         console.log(insertedHandover)
//         handover.Contents.map(content => {
//             content.handover_id = insertedHandover.id
//         })
//         await Content.bulkCreate(handover.Contents);
//         await transaction.commit();

//         return true
//     } catch (e) {
//         await transaction.rollback();
//         console.log(e)
//         return false
//     }
// }

// export async function updateHandover(handover){
//     return mysql.transaction(t => {
//         return Handover.update(handover, {
//             where: {
//                 id: {
//                     [Op.eq]: handover.id
//                 }
//             },
//             transaction: t
//         })
//             .then(() => {
//                 for(let content in handover.Contents){
//                     if (content.id) {
//                         return Content.update(content, {transaction: t})
//                     } else {
//                         content.handover_id = handover.id
//                         return Content.create(content, {transaction: t})
//                     }
//                 }
//             })
//     })
// }

// export async function getHandoverListWeek(store_id, begin, end) {
//     return await Handover.findAll({
//         include: [
//             {
//                 model: Content
//             },
//             {
//                 model: User,
//                 where: {
//                     store_id: {
//                         [Op.eq]: store_id
//                     }
//                 },
//                 attributes: ['id', 'name']
//             }
//         ],
//         where: {
//             date: {
//                 [Op.between]: [begin, end]
//             }
//         }
//     })
// }
